#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 50;
int n, m;
int g[N][N], d[N][N];
PII path[N][N];

int bfs()
{
    queue<PII> q;

    memset(d, -1, sizeof d);
    d[0][0] = 0;
    q.push({ 0, 0 });

    int dx[] = { 0,0,-1,1 }, dy[] = { -1,1,0,0 };

    while (q.size())
    {
        auto t = q.front();
        q.pop();

        for (int i = 0; i < 4; i++)
        {
            int x = t.first + dx[i], y = t.second + dy[i];

            if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1)
            {
                path[x][y].first = t.first;
                path[x][y].second = t.second;
                d[x][y] = d[t.first][t.second] + 1;
                q.push({ x, y });
            }
        }
    }

    return d[n - 1][m - 1];
}

int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> g[i][j];

    cout << bfs() << '\n';


    // cout<<n-1<<" "<<m-1<<'\n';
    // int x=path[n-1][m-1].first;
    // int y=path[n-1][m-1].second;
    int x = n - 1;
    int y = m - 1;
    while (x||y)
    {
        cout << x << " " << y << '\n';
        int x2 = x,y2=y;
        x = path[x2][y2].first;
        y = path[x2][y2].second;
    }
    cout << 0 << " " << 0 << '\n';

    return 0;
}

 //5 6
 //0 1 0 0 0 0
 //0 0 0 1 0 0
 //0 0 1 0 0 1
 //1 1 0 0 0 0
 //1 1 0 0 0 0

